package com.debao;

import com.debao.common.annotation.Anonymous;
import com.debao.common.constant.Constants;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.*;
import com.debao.system.service.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 首页
 */
@RestController
@RequestMapping("/api/index")
public class ApiIndex {
    @Resource
    private IBannerService bannerService;
    @Resource
    private ICoachService coachService;
    @Resource
    private IStadiumService stadiumService;
    @Resource
    private IExcellentStudentService excellentStudentService;
    @Resource
    private ISysUserService userService;

    /**
     * 首页数据
     *
     * @return
     */
    @GetMapping("/info")
    @Anonymous
    public AjaxResult index() {
        Long userId = SecurityUtils.getLoginUserId();
        String isFollow = "N";
        try {
            if (userId != null) {
                isFollow = userService.selectIsFollowByUserId(userId);
            }
            HashMap<String, Object> data = new HashMap<>();
            // 轮播图
            data.put("banner", bannerService.selectBannerList(new Banner()));
            // 热门教练
            data.put("coach", coachService.selectCoachList(new Coach().setStatus(Constants.STATUS_1).setIsDel(Constants.IS_DEL_N)));
            // 场馆
            data.put("stadium", stadiumService.selectStadiumList(new Stadium().setStatus(1)));
            // 优秀学员
            data.put("student", excellentStudentService.selectExcellentStudentList(new ExcellentStudent().setLimit(3)));
            data.put("isFollow", isFollow);
            return AjaxResult.success(data);
        } catch (Exception e) {
            // 记录异常信息（一般可以使用日志框架记录）
            return AjaxResult.error("获取首页数据失败: " + e.getMessage());
        }
    }
}
